package com.ayoka.ui.launch;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.base.BaseActivity;
import com.ayoka.databinding.ActivityJoinBinding;
import com.ayoka.mvp.JoinMVP.IJoinView;
import com.ayoka.mvp.JoinMVP.JoinPresenter;
import com.ayoka.ui.main.MainActivity;
import com.ayoka.utils.WeChatManager;
import com.jcp.baselibrary.utils.SPUtil;
import com.jcp.baselibrary.utils.ToastUtil;
import com.jcp.baselibrary.utils.UIUtils;

import java.util.Observable;

/**
 * =========================================================
 * 作者： 金春平
 * 类名： GuideActivity
 * 类描述：
 * 创建日期 17/03/30
 * =========================================================
 */
public class JoinActivity extends BaseActivity<ActivityJoinBinding, JoinPresenter> implements IJoinView {

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, JoinActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onBeforeSetContentLayout() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_join;
    }

    @Override
    protected void initViews() {
        mDataBinding.btnNeed.setOnClickListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mPresenter.getJoinCount();
    }

    @Override
    protected JoinPresenter getPresenter() {
        return new JoinPresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnNeed){
            WeChatManager.getInstance().addObserver(this);
            WeChatManager.getInstance().startLogin();
            mDataBinding.btnNeed.setEnabled(false);
        }
    }

    @Override
    public void configJoinCount(Long data) {
        String text = String.format(getString(R.string.format_join_number), data);
        mDataBinding.tvJoinNumber.setText(text);
    }

    @Override
    public void onLoginCallback(boolean isSuccess) {
        if (isSuccess) {
            ToastUtil.getInstance().show(getString(R.string.wechat_login_success));
            MainActivity.start(JoinActivity.this);
            finish();
        } else {
            ToastUtil.getInstance().show(getString(R.string.wechat_login_failure));
            mDataBinding.btnNeed.setEnabled(true);
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof WeChatManager) {
            int operation = (int) data;
            if (WeChatManager.WE_CHAT_LOGIN_START == operation) {
                showWaitDialog(getString(R.string.wechat_login_start));
            } else if (WeChatManager.WE_CHAT_LOGIN_SUCCESS == operation) {
                String openId = SPUtil.getString(Constant.WX_OPEN_ID, "");
                mPresenter.loginByWechat(openId);
            }else if (WeChatManager.WE_CHAT_LOGIN_FILED == operation) {
                ToastUtil.getInstance().show(getString(R.string.wechat_login_failure));
                mDataBinding.btnNeed.setEnabled(true);
                hideWaitDialog();
            } else if (WeChatManager.WE_CHAT_LOGIN_CANCEL == operation) {
                ToastUtil.getInstance().show(getString(R.string.user_cancel_login));
                mDataBinding.btnNeed.setEnabled(true);
                hideWaitDialog();
            }
        }
    }

    @Override
    protected void onDestroy() {
        WeChatManager.getInstance().deleteObserver(this);
        super.onDestroy();
    }

}
